package com.desuntech.components
{
	import flash.display.Sprite;
	import flash.events.ProgressEvent;
	
	import mx.controls.ProgressBar;
	import mx.controls.ProgressBarMode;
	import mx.core.FlexGlobals;
	import mx.managers.PopUpManager;
	
	import spark.components.BorderContainer;
	
	public class LoadingMask extends BorderContainer
	{
		private static var _mask:LoadingMask;
		
		private var _message:String;
		private var _pb:ProgressBar;
		
		/**
		 * To use it is as simple as this:
		 * Mask.show("Please wait...");
		 * When you're done you do this:
		 * Mask.close();
		 */
		
		public function LoadingMask()
		{
			super();
		}
		
		public static function show(message:String, parent:Sprite=null):LoadingMask{
			
			_mask = new LoadingMask();
			_mask._message = message;
			PopUpManager.addPopUp(_mask, parent||Sprite(FlexGlobals.topLevelApplication), true);
			PopUpManager.centerPopUp(_mask);
			
			return _mask;	
		}
		
		
		public static function close():void {
			PopUpManager.removePopUp(_mask);
		}
		
		public static function setProgress(e:ProgressEvent):void {
			_mask._pb.setProgress(e.bytesLoaded, e.bytesTotal);
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			_pb = new ProgressBar();
			_pb.label = (_message||'请稍候...') + '   (%3 %%)';
			_pb.mode = ProgressBarMode.MANUAL;
			_pb.labelPlacement= 'center';
			_pb.height = 28;
			addElement(_pb);
		}
	}
}